package com.fs.service;

import com.fs.entity.Shop;
import com.fs.repository.CleanerRepository;
import com.fs.repository.RoomRepository;
import com.fs.repository.ShopRepository;
import com.fs.service.bo.BoPageInfo;
import com.fs.service.bo.shop.BoCreateShopParam;
import com.fs.service.bo.shop.BoShop;
import com.fs.utils.Utils;
import org.aspectj.weaver.ast.Not;
import org.springframework.data.domain.*;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional
@Validated
public class ShopService {
    ShopRepository shopDao;
    CleanerRepository cleanerDao;
    RoomRepository roomDao;

    public ShopService(ShopRepository shopDao, CleanerRepository cleanerDao, RoomRepository roomDao) {
        this.shopDao = shopDao;
        this.cleanerDao = cleanerDao;
        this.roomDao = roomDao;
    }

    public BoShop getShopByID(Integer id) {
        Optional<Shop> opt = shopDao.findById(id);
        if (opt.isPresent()) {
            return new BoShop(opt.get());
        } else {
            throw new NotFoundException("没有找到门店");
        }
    }

    /*
     * 创建门店
     */
    public BoShop createShop(@Valid BoCreateShopParam shopInfo) {

        Shop shop = new Shop();
        shop.setName(shopInfo.getName());
        shop.setLatitude(shopInfo.getLatitude());
        shop.setLongitude((shopInfo.getLongitude()));
        shopDao.save(shop);

        return new BoShop(shop);
    }

    /***
     * 删除门店
     * @param id 门店id
     */
    public void deleteShop(int id) {
        if (id < 1) {
            throw new BadParamException("门店id不能传空");
        }
        Optional<Shop> optShop = shopDao.findById(id);
        if (!optShop.isPresent()) {
            throw new NotFoundException("找不到此门店");
        }

        Long cleanerCount = cleanerDao.countByShop(optShop.get());
        Long roomCount = roomDao.countByShop(optShop.get());
        if (cleanerCount > 0 || roomCount > 0) {
            throw new CanNotDeleteException("保洁或房间存在关联,不能删除");
        }
        shopDao.deleteById(id);
    }

    /**
     * 获取门店信息
     *
     * @param shopName 筛选条件
     * @param pageNo   页码 可以为0,获取所有信息
     * @param pageSize 页大小 可以为0,获取所有信息
     */
    public BoPageInfo<BoShop> getShopByPage(@Nullable String shopName, int pageNo, int pageSize) {
        Shop shop = new Shop();
        Example<Shop> example = Example.of(shop);
        if (shopName != null) {
            shop.setName(shopName);
        }

        if (pageNo > 0 && pageSize > 0) {
            Pageable pageable = PageRequest.of(--pageNo, pageSize, Sort.by(Sort.Direction.DESC, "id"));
            Page<Shop> result = shopDao.findAll(example, pageable);
            List<BoShop> list = result.getContent().stream()
                    .map(BoShop::new).collect(Collectors.toList());
            return new BoPageInfo<>(list, result.getTotalElements());
        } else {
            List<Shop> allShop = shopDao.findAll(example);
            List<BoShop> shopInfos = allShop.stream()
                    .map(BoShop::new).collect(Collectors.toList());
            return new BoPageInfo<>(shopInfos, null);
        }

    }

}
